#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
链路追踪数据模型
"""

from datetime import datetime
from bson import ObjectId

class TraceModel:
    """链路追踪数据模型"""
    
    def __init__(self, trace_id=None, request_definition=None, trace_data=None):
        self.id = str(ObjectId()) if trace_id is None else trace_id
        self.request_definition = request_definition or {}
        self.trace_data = trace_data or []
        self.created_at = datetime.utcnow()
        self.updated_at = datetime.utcnow()
        
    def to_dict(self):
        """转换为字典"""
        return {
            "_id": self.id,
            "request_definition": self.request_definition,
            "trace_data": self.trace_data,
            "created_at": self.created_at,
            "updated_at": self.updated_at
        }
    
    @classmethod
    def from_dict(cls, data):
        """从字典创建对象"""
        if not data:
            return None
        
        trace = cls(
            trace_id=data.get("_id"),
            request_definition=data.get("request_definition", {}),
            trace_data=data.get("trace_data", [])
        )
        trace.created_at = data.get("created_at", datetime.utcnow())
        trace.updated_at = data.get("updated_at", datetime.utcnow())
        return trace


class ServiceModel:
    """服务模型"""
    
    def __init__(self, service_id=None, trace_id=None, service_name=None):
        self.id = str(ObjectId()) if service_id is None else service_id
        self.trace_id = trace_id
        self.service_name = service_name
        self.created_at = datetime.utcnow()
        
    def to_dict(self):
        """转换为字典"""
        return {
            "_id": self.id,
            "trace_id": self.trace_id,
            "service_name": self.service_name,
            "created_at": self.created_at
        }
    
    @classmethod
    def from_dict(cls, data):
        """从字典创建对象"""
        if not data:
            return None
        
        service = cls(
            service_id=data.get("_id"),
            trace_id=data.get("trace_id"),
            service_name=data.get("service_name")
        )
        service.created_at = data.get("created_at", datetime.utcnow())
        return service


class TopologyModel:
    """拓扑信息模型"""
    
    def __init__(self, topology_id=None, service_id=None, parent_id=None):
        self.id = str(ObjectId()) if topology_id is None else topology_id
        self.service_id = service_id
        self.parent_id = parent_id
        self.created_at = datetime.utcnow()
        
    def to_dict(self):
        """转换为字典"""
        return {
            "_id": self.id,
            "service_id": self.service_id,
            "parent_id": self.parent_id,
            "created_at": self.created_at
        }
    
    @classmethod
    def from_dict(cls, data):
        """从字典创建对象"""
        if not data:
            return None
        
        topology = cls(
            topology_id=data.get("_id"),
            service_id=data.get("service_id"),
            parent_id=data.get("parent_id")
        )
        topology.created_at = data.get("created_at", datetime.utcnow())
        return topology


class RequestModel:
    """请求信息模型"""
    
    def __init__(self, request_id=None, service_id=None, request_type=None, method=None, url=None):
        self.id = str(ObjectId()) if request_id is None else request_id
        self.service_id = service_id
        self.request_type = request_type  # "ingress" 或 "egress"
        self.method = method
        self.url = url
        self.created_at = datetime.utcnow()
        
    def to_dict(self):
        """转换为字典"""
        return {
            "_id": self.id,
            "service_id": self.service_id,
            "request_type": self.request_type,
            "method": self.method,
            "url": self.url,
            "created_at": self.created_at
        }
    
    @classmethod
    def from_dict(cls, data):
        """从字典创建对象"""
        if not data:
            return None
        
        request = cls(
            request_id=data.get("_id"),
            service_id=data.get("service_id"),
            request_type=data.get("request_type"),
            method=data.get("method"),
            url=data.get("url")
        )
        request.created_at = data.get("created_at", datetime.utcnow())
        return request
